package com.yuyou.tas.admin.service.impl;

import com.yuyou.tas.admin.mapper.AuthUserSectionMapper;
import com.yuyou.tas.admin.service.IAuthUserSectionService;
import com.yuyou.tas.service.api.admin.dto.AuthUserSectionDTO;
import com.yuyou.tas.service.api.admin.entity.AuthUserSection;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class AuthUserSectionServiceImpl implements IAuthUserSectionService{

	@Autowired
	private AuthUserSectionMapper authUserSectionMapper;
	
	@Override
	public int deleteByPrimaryKey(Long id) {
		return authUserSectionMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(AuthUserSection record) {
		return authUserSectionMapper.insert(record);
	}

	@Override
	public AuthUserSection selectByPrimaryKey(Long id) {
		return authUserSectionMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(AuthUserSection record) {
		return authUserSectionMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public List<AuthUserSectionDTO> findSectionByUserId(Long authUserId) {
		return authUserSectionMapper.findSectionByAuthUserId(authUserId);
	}

	@Override
	public List<AuthUserSectionDTO> findSectionByUserIds(List<Long> authUserIds) {
		return authUserSectionMapper.findSectionByAuthUserIds(authUserIds);
	}

	@Override
	public Set<Long> findUniSectionByUserId(Long userId) {
        Set<Long>sectionSet=new HashSet<>();
        List<String> sectionStrList=this.findSectionByUserId(userId).stream().map(item->item.getBaseSectionId()).collect(Collectors.toList());
        for(String str:sectionStrList){
        	List<Long> baseSectionIds = Arrays.asList(str.split(",")).stream().filter(f-> !StringUtils.isEmpty(f)).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
        	sectionSet.addAll(baseSectionIds);
        }
		return sectionSet;
	}

	@Override
	public AuthUserSection findSectionByUserIdSubjectId(Long authUserId, Long dataSubject) {
		return authUserSectionMapper.findSectionByUserIdSubjectId(authUserId, dataSubject);
	}

	@Override
	public Integer findCountByParams(Long authUserId, Long dataSubject, Long sectionId) {
		return authUserSectionMapper.findCountByParams(authUserId , dataSubject ,sectionId);
	}

}